{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "from SpiderUtils.myUtils import myFakerHeaders\n",
    "from bs4 import BeautifulSoup\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "def get_html(url):\n",
    "    header = {'User-Agent': myFakerHeaders.getFakeHeaders()}\n",
    "    # p = myFakerHeaders.getFakeProxy()\n",
    "    req = requests.get(url=url, headers=header)\n",
    "    # 因为又是后是图片，就不要返回text了\n",
    "    return req"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "def is_girls(tags):\n",
    "    if \"girl\" in tags:\n",
    "        return True\n",
    "    elif \"women\" in tags:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "def get_img(src):\n",
    "    html = get_html(src)\n",
    "    bs = BeautifulSoup(html.text, parser=\"html.parser\")\n",
    "    img = re.findall(r\"https://w.wallhaven.cc/full.*.png\", str(bs.select(\"#wallpaper\")))\n",
    "    print(img)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['https://w.wallhaven.cc/full/y8/wallhaven-y8ql7g.png']\n"
     ]
    }
   ],
   "source": [
    "get_img(\"https://wallhaven.cc/w/y8ql7g\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "[]\n",
      "[]\n",
      "[]\n",
      "[]\n",
      "[]\n",
      "[]\n",
      "['https://w.wallhaven.cc/full/8o/wallhaven-8ogw7y.png\" data-wallpaper-height=\"1300\" data-wallpaper-id=\"8ogw7y\" data-wallpaper-width=\"930\" id=\"wallpaper\" style=\"display:none;visibility:hidden;\"/>, <img alt=\"Anime 930x1300 anime anime girls drink original characters short hair vertical yomu\" crossorigin=\"anonymous\" data-wallpaper-height=\"1300\" data-wallpaper-id=\"8ogw7y\" data-wallpaper-width=\"930\" id=\"wallpaper\" src=\"https://w.wallhaven.cc/full/8o/wallhaven-8ogw7y.png']\n",
      "[]\n",
      "['https://w.wallhaven.cc/full/1k/wallhaven-1k1jw3.png']\n",
      "['https://w.wallhaven.cc/full/y8/wallhaven-y8ql7g.png']\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 2):\n",
    "    url = \"https://wallhaven.cc/toplist?page={}\".format(i)\n",
    "\n",
    "    html = get_html(url)\n",
    "    bs = BeautifulSoup(html.text, parser=\"lxml\")\n",
    "    sections = bs.find_all(\"section\", class_=\"thumb-listing-page\")\n",
    "    for s in sections:\n",
    "        imgs_url = s.find_all(\"li\")\n",
    "        for img in imgs_url:\n",
    "            href = img.a['href']\n",
    "            html1 = get_html(href)\n",
    "            bs1 = BeautifulSoup(html1.text, parser=\"lxml\")\n",
    "            len(bs1.find_all(\"ul\", id=\"tags\"))\n",
    "            tags = bs1.find_all(\"ul\", id=\"tags\")\n",
    "            tags_str = \"\"\n",
    "            for tag in tags:\n",
    "                ts = tag.find_all(\"li\")\n",
    "                for t in ts:\n",
    "                    tags_str += t.a.text\n",
    "                # 2. 进入页面，判断是否为女孩\n",
    "                if is_girls(tags_str):\n",
    "                    get_img(href)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}